package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.JSONResult;
import jdk.nashorn.api.scripting.AbstractJSObject;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IDestinationService destinationService;

    @RequestMapping("list")
    public  String list(@ModelAttribute("qo")DestinationQueryObject qo, Model model){
        if(qo.getParentId()>0){
            List<Destination> destinations = destinationService.listToasts(qo.getParentId());
            model.addAttribute("toasts",destinations);
        }

        model.addAttribute("pageInfo",destinationService.queryForList(qo));
        return "destination/list";
    }

    @RequestMapping("setInfo")
    @ResponseBody
    public  JSONResult setInfo(Destination destination){
        JSONResult json = new JSONResult();
        try {
            destinationService.updateByPrimaryKey(destination);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
    @RequestMapping("changeHotState")
    @ResponseBody
    public  JSONResult changeHotState(Long id,Integer hot){
        JSONResult json = new JSONResult();
        try {
            destinationService.changeHot(id,hot);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

}
